// https://leetcode.cn/problems/global-and-local-inversions/
// Created by ade on 2022/11/16.
//
#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
    // 核心思路：局部倒置必然是全局倒置，所以不能有全局倒置的跨度大于1
    bool isIdealPermutation(vector<int> &nums) {
        int maxI = nums[0];
        for (int i = 2; i < nums.size(); i++) {
            maxI = max(nums[i - 2], maxI); // 这里是核心，必须先算maxI
            if (maxI > nums[i]) return false;
        }
        return true;
    }
};

int main() {
    Solution so;
    vector<int> nums = {4, 10, 15, 24, 26};
    auto res = so.isIdealPermutation(nums);
    cout << res << endl;
    return 0;
}